package demoMod.invincibleOne.powers;

import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.animations.VFXAction;
import com.megacrit.cardcrawl.actions.common.ReducePowerAction;
import com.megacrit.cardcrawl.actions.common.RemoveSpecificPowerAction;
import com.megacrit.cardcrawl.actions.unique.RemoveAllPowersAction;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.localization.PowerStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.powers.BufferPower;
import com.megacrit.cardcrawl.vfx.combat.WeightyImpactEffect;
import demoMod.invincibleOne.InvincibleOneMod;

import java.util.stream.Collectors;

public class RagnarokPower extends AbstractPower {
    public static final String POWER_ID = InvincibleOneMod.makeID("RagnarokPower");
    private static final PowerStrings powerStrings = CardCrawlGame.languagePack.getPowerStrings(POWER_ID);
    public static final String NAME = powerStrings.NAME;
    public static final String[] DESC = powerStrings.DESCRIPTIONS;
    private static int idOffset = 0;

    public RagnarokPower(AbstractCreature owner, int amount) {
        this.owner = owner;
        this.ID = POWER_ID + idOffset;
        idOffset++;
        this.name = NAME;
        this.amount = amount;
        loadRegion("end_turn_death");
        this.updateDescription();
        this.type = PowerType.BUFF;
        this.priority = 0;
    }

    public void updateDescription() {
        this.description = String.format(DESC[0], this.amount);
    }

    @Override
    public void atStartOfTurn() {
        if (this.amount <= 1) {
            addToBot(new AbstractGameAction() {
                @Override
                public void update() {
                    (AbstractDungeon.getCurrRoom()).cannotLose = false;
                    for (AbstractMonster m2 : AbstractDungeon.getMonsters().monsters.stream().filter(m2 -> !m2.isDeadOrEscaped() || m2.halfDead).collect(Collectors.toList())) {
                        addToBot(new RemoveAllPowersAction(m2, false));
                        addToBot(new AbstractGameAction() {
                            @Override
                            public void update() {
                                m2.currentHealth = 0;
                                m2.healthBarUpdatedEvent();
                                m2.die();
                                isDone = true;
                            }
                        });
                    }
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            AbstractDungeon.getCurrRoom().endBattle();
                        }
                    });
                    isDone = true;
                }
            });
        } else {
            addToBot(new ReducePowerAction(this.owner, this.owner, ID, 1));
        }
    }
}
